<template>
	<view id="view">
		<u-swiper :list="swiperList"></u-swiper>
		<u-notice-bar text="师生需要连续14天打卡才可以通行" 
					  color="var(--theme-color)">
		</u-notice-bar>
		<u-cell title="点击进行定位" 
				:disabled="disable"
				:value="longitude == null ? '未定位': `(${longitude},${latitude})`"
				isLink clickable 
				@click="getLocation">
		</u-cell>
		<u-cell title="健康状况"
				:disabled="disable"
				:value="health"
				isLink clickable 
				@click="showPicker = true">
		</u-cell>
		<u-picker :show="showPicker" 
				  @close="showPicker = false" 
				  @cancel="showPicker = false" 
				  @confirm="chooseHealth"
				  :columns="healthStatus" 
				  closeOnClickOverlay 
				  confirmColor="var(--theme-color)">
		</u-picker>
		<u--textarea :disabled="disable" v-model="remark" placeholder="备注(选填)" ></u--textarea>
		<view style="width: 80vw;margin:10upx auto">
			<u-button @click="submit" :disabled="disable" type="primary" :text="disable?'今日已打卡':'提交'" color="var(--theme-color)"></u-button>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList:[
                    'https://www.nefu.edu.cn/images/2022n/20220616biyedianli.jpg',
                    'https://www.nefu.edu.cn/images/20211222xiaoqing.jpg',
                    'https://www.nefu.edu.cn/images/2022n/20220603duanwujie.jpg',
                ],
				longitude:null,
				latitude:null,
				health:"正常",
				showPicker:false,
				healthStatus:[['正常','异常']],
				remark:'',
				disable:false
			}
		},
		onLoad() {
			const that = this
			let account = uni.getStorageSync("account")
			let password = uni.getStorageSync("password")
			if(account.length!=0 && password.length!=0){
				uni.request({
					url:that.myConfig.apiBaseUrl+that.myConfig.apiProxy+"/auth/login",
					method:'post',
					data:{
						account:account,
						password:password
					},
					success:function(res){
						if(res.data.code==0){
							that.myData.token = res.data.data.token
							uni.setStorageSync("name", res.data.data.info.name)
							uni.setStorageSync("position", res.data.data.info.position)
							that.API({
								showLoading:true,
								url:"/user/today",
								code0:function(res){
									if(res.data.data.hasOwnProperty("id")){
										that.disable = true
										that.longitude = res.data.data.longitude
										that.latitude = res.data.data.latitude
										that.health = res.data.data.health
										that.remark = res.data.data.remark!=null?res.data.data.remark:""
									}
								}
							})
						}else{
							uni.redirectTo({
								url:"/pages/login/login"
							})
						}
					},
					fail:function(){
						uni.redirectTo({
							url:"/pages/login/login"
						})
					}
				})
			}else{
				uni.redirectTo({
					url:"/pages/login/login"
				})
			}
			
		},
		methods: {
			getLocation(){
				const that = this
				uni.getLocation({
					success: function (res) {
						that.longitude = res.longitude
						that.latitude = res.latitude
					}
				});
			},
			chooseHealth(status){
				const that = this
				that.health = status.value[0]
				that.showPicker = false
			},
			submit(){
				const that = this
				if(that.longitude==null){
					uni.showToast({
						title:"请完善信息后提交",
						icon:"error"
					})
					return
				}
				that.API({
					showLoading:true,
					url:"/user/today",
					method:'put',
					data:{
						longitude:that.longitude,
						latitude:that.latitude,
						health:that.health,
						remark:that.remark
					},
					code0:function(res){
						if(res.data.data!={}){
							that.disable = true
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
